{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c712ccbd",
   "metadata": {},
   "source": [
    "# 字符串 Strings"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8dc4c3e6",
   "metadata": {},
   "source": [
    "## 1.字符串是什么\n",
    "\n",
    "* 字符串是序列字符的数组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24b69a8b",
   "metadata": {},
   "source": [
    "## 2.字符串的一般表现形式"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4e8adadd",
   "metadata": {},
   "source": [
    "> 1.单引号 ''\n",
    "\n",
    "> 2.双引号 \"\"\n",
    "\n",
    "> 3.三引号 \"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c9b6f72f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'spa, eggs'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'spa, eggs' # single quotes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e947dbbc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"doesn't\""
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'doesn\\'t'  # use '\\' to escape the single quote"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f77968b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"doesn't\""
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"doesn't\"  # ... or use double quotes instead"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "79e764aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\"Yes,\" they said.'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'\"Yes,\" they said.'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e56dfef9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\"Yes,\" they said.'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\\\"Yes,\\\" they said.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ebdf8ce8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\"Isn\\'t,\" they said.'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'\"Isn\\'t,\" they said.'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7793eaf",
   "metadata": {},
   "source": [
    "## 3.字符串的运算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a58ee365",
   "metadata": {},
   "source": [
    "> 1.+ 连接字符串 * 重复运算符\n",
    "\n",
    "> 2.索引：Python中的字符串有两种索引方式，从左往右以0开始，从右往左以-1开始。\n",
    "\n",
    "> 3.取值：字符串取多个值(切片)\n",
    "\n",
    "> 4.切片：切片（Slice）是一个取部分元素的操作，是Python中特有的功能。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e28ca8b",
   "metadata": {},
   "source": [
    "## 4.字符串的一般方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1021dddd",
   "metadata": {},
   "source": [
    "> 1.字符串拼接：join\n",
    "\n",
    "> 2.查找元素的位置：find\n",
    "\n",
    "> 3.查找元素的个数：count\n",
    "\n",
    "> 4.替换字符：replace\n",
    "\n",
    "> 5.字符串分割：split\n",
    "\n",
    "> 6.格式化输出：format\n",
    "\n",
    "> 7.将字母大写：upper\n",
    "\n",
    "> 8.将字母转为小写：lower"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d691b025",
   "metadata": {},
   "source": [
    "* 1.字符串拼接 join"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a9bc2cd1",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = ['狗子', '二丫', '三', '四']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "b323c673",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = ','.join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "da4ee3ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "狗子,二丫,三,四\n"
     ]
    }
   ],
   "source": [
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "74057dc3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------\n"
     ]
    }
   ],
   "source": [
    "print('-'*20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "5cecce8b",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = '狗子'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "e7d52e1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = '三ge'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "2a24a4b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "狗子三ge\n"
     ]
    }
   ],
   "source": [
    "print(a+b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a110624e",
   "metadata": {},
   "source": [
    "* 2.查找元素的位置 find"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "6d1fb27c",
   "metadata": {},
   "outputs": [],
   "source": [
    "zfc = 'abcdefgh'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "4fc828b0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "print(zfc.find('e'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "40f3f7a2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "print(zfc.find('de'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "ad43cda1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-1\n"
     ]
    }
   ],
   "source": [
    "print(zfc.find('1'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c115ea09",
   "metadata": {},
   "source": [
    "* 3.替换字符 replace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "9f68842c",
   "metadata": {},
   "outputs": [],
   "source": [
    "zfc = 'abcde:fgh'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "154df311",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abcde,,,,fgh\n"
     ]
    }
   ],
   "source": [
    "print(zfc.replace(':',',,,,'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "bb0ea92f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abcde:fgh\n"
     ]
    }
   ],
   "source": [
    "print(zfc.replace('ss',',,,,'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "b6df1d7f",
   "metadata": {},
   "outputs": [],
   "source": [
    "zfc = 'abcde:fgha'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "bc55f651",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "111bcde:fgh111\n"
     ]
    }
   ],
   "source": [
    "print(zfc.replace('a','111'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "d03e2571",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "111bcde:fgha\n"
     ]
    }
   ],
   "source": [
    "print(zfc.replace('a','111',1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eca3f704",
   "metadata": {},
   "source": [
    "* 4.字符串的分割 split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "903bf36e",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = '狗子 二丫 张三 李四'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "410ee03d",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = s.split()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "483858f3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['狗子', '二丫', '张三', '李四']\n"
     ]
    }
   ],
   "source": [
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c14f221",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
